package com.example.composedialogdemo.dialog

import android.app.TimePickerDialog
import android.icu.util.Calendar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import java.time.LocalTime

/**
 * @author: njb
 * @date:   2025/8/17 0:36
 * @desc:   描述
 */
@Composable
fun TimePickerDialogSample(
    showDialog: Boolean,
    onDismiss: () -> Unit,
    onTimeSelected: (LocalTime) -> Unit
) {
    val context = LocalContext.current
    val calendar = Calendar.getInstance()
    val hour = calendar.get(Calendar.HOUR_OF_DAY)
    val minute = calendar.get(Calendar.MINUTE)
    val timePicker = remember {
        TimePickerDialog(
            context,
            { _, selectedHour, selectedMinute ->
                onTimeSelected(LocalTime.of(selectedHour, selectedMinute))
            },
            hour,
            minute,
            true // 24小时制
        )
    }
    if (showDialog) {
        DisposableEffect(Unit) {
            timePicker.show()
            onDispose {
                timePicker.dismiss()
            }
        }
    }
}